package com.asman.worker.rn

import androidx.core.app.NotificationManagerCompat
import com.alibaba.sdk.android.push.CommonCallback
import com.asman.push.PushUtils
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import java.util.*

/**
 * author : ybk
 * date   : 2019-07-18 19:47
 * desc   :
 */
class NativePush(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

    override fun getName(): String {
        return "NativePush"
    }

    override fun getConstants(): Map<String, Any>? {
        return HashMap()
    }

    /**
     * 检查通知开关是否开启
     * @param promise Promise
     * @return -1：未开启，1：已开启
     */
    @ReactMethod
    fun checkStatus(promise: Promise) {
        val isOpen = NotificationManagerCompat.from(reactApplicationContext).areNotificationsEnabled()
        if (isOpen) {
            promise.resolve(1)
        } else {
            promise.resolve(-1)
        }
    }

    /**
     * 添加别名
     * @param alias 需要添加的别名
     * @param promise Promise
     * @return -1：添加失败，1：添加成功
     */
    @ReactMethod
    fun addAlias(alias: String, promise: Promise) {
        PushUtils.setAlias(alias, object : CommonCallback {
            override fun onSuccess(p0: String?) {
                promise.resolve(1)
            }

            override fun onFailed(p0: String?, p1: String?) {
                promise.resolve(-1)
            }
        })
    }

    /**
     * 移除指定别名 或者 移除所有别名
     * @param alias 需要移除的别名，如果为空，移除所有别名
     * @param promise Promise
     * @return -1：移除失败，1：移除成功
     */
    @ReactMethod
    fun removeAlias(alias: String?, promise: Promise) {
        PushUtils.removeAlias(alias, object : CommonCallback {
            override fun onSuccess(p0: String?) {
                promise.resolve(1)
            }

            override fun onFailed(p0: String?, p1: String?) {
                promise.resolve(-1)
            }
        })
    }
}
